package com.tusuo.stds.ui.level;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelDetail;
import com.tusuo.stds.rest.RankList;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import at.grabner.circleprogress.CircleProgressView;
import at.grabner.circleprogress.UnitPosition;
import cn.carbs.android.avatarimageview.library.AvatarImageView;

/**
 * Created by developer on 17-8-30.
 */

@EFragment(R.layout.frag_level)
public class LevelFragment extends Fragment {

    @ViewById(R.id.frag_level_layout_description)
    LinearLayout layoutDescription;
    @ViewById(R.id.frag_level_tv_description)
    TextView tvDescription;

    @ViewById(R.id.frag_level_layout_closed)
    LinearLayout layoutClosed;
    @ViewById(R.id.frag_level_tv_closed_tips)
    TextView tvClosedTips;
    @ViewById(R.id.frag_level_btn_go_to_dependency)
    Button btnGoToDependency;

    @ViewById(R.id.frag_level_mode_must_pass_layout)
    LinearLayout layoutModeMustPass;
    @ViewById(R.id.frag_level_mode_must_pass_layout_not_pass)
    LinearLayout layoutModeMustPassNotPass;
    @ViewById(R.id.frag_level_mode_must_pass_layout_perfect_pass)
    LinearLayout layoutModeMustPassPerfectPass;
    @ViewById(R.id.frag_level_mode_must_pass_tv_created_time)
    TextView tvModeMustPassThroughTime;
    @ViewById(R.id.frag_level_mode_must_pass_tv_clearance_time)
    TextView tvModeMustPassClearanceTime;

    @ViewById(R.id.frag_level_mode_can_pass_layout)
    LinearLayout layoutModeCanPass;
    @ViewById(R.id.frag_level_mode_can_pass_layout_pass)
    RelativeLayout layoutModeCanPassPass;
    @ViewById(R.id.frag_level_mode_can_pass_tv_status)
    TextView tvModeCanPassStatus;
    @ViewById(R.id.frag_level_mode_can_pass_tv_created_time)
    TextView tvModeCanPassThroughTime;
    @ViewById(R.id.frag_level_mode_can_pass_tv_not_pass)
    TextView tvModeCanPassNotPass;

    @ViewById(R.id.frag_level_mode_can_pass_iv_star_1)
    ImageView ivModeCanPassStar1;
    @ViewById(R.id.frag_level_mode_can_pass_iv_star_2)
    ImageView ivModeCanPassStar2;
    @ViewById(R.id.frag_level_mode_can_pass_iv_star_3)
    ImageView ivModeCanPassStar3;
    @ViewById(R.id.frag_level_mode_can_pass_circleview)
    CircleProgressView circleProgressViewModeCanPass;
    @ViewById(R.id.frag_level_mode_can_pass_tv_clearance_time)
    TextView tvModeCanPassClearanceTime;
    @ViewById(R.id.frag_level_mode_can_pass_btn_begin)
    Button btnModeCanPassBegin;
    @ViewById(R.id.frag_level_mode_must_pass_btn_begin)
    Button btnModeMustPassBegin;
    @ViewById(R.id.frag_level_mode_must_pass_btn_again)
    Button btnModeMustPassAgain;

    @ViewById(R.id.frag_level_mode_can_pass_tv_persent)
    TextView tvPersent;

    @ViewById(R.id.frag_level_aiv_best_avatar)
    AvatarImageView aivBestAvatar;
    @ViewById(R.id.frag_level_iv_best_star1)
    ImageView ivBestStar1;
    @ViewById(R.id.frag_level_iv_best_star2)
    ImageView ivBestStar2;
    @ViewById(R.id.frag_level_iv_best_star3)
    ImageView ivBestStar3;
    @ViewById(R.id.frag_level_tv_best_name)
    TextView tvBestName;
    @ViewById(R.id.frag_level_tv_best_school)
    TextView tvBestSchool;
    @ViewById(R.id.frag_level_tv_best_time)
    TextView tvBestTime;
    @ViewById(R.id.frag_level_layout_best)
    LinearLayout layoutBest;

    @FragmentArg("levelDetail")
    LevelDetail levelDetail;

    private RankList.Rank bestRank;
    private boolean firstIntent = true;

    @Override
    public void onResume() {
        if (!firstIntent) {
            initData();
        }

        super.onResume();
    }

    public void hideLayout() {
        layoutClosed.setVisibility(View.GONE);
        layoutModeMustPass.setVisibility(View.GONE);
        layoutModeCanPass.setVisibility(View.GONE);
        layoutBest.setVisibility(View.GONE);
    }

    @AfterViews
    void initData() {
        hideLayout();

        if (TextUtils.isEmpty(levelDetail.Description)) {
            layoutDescription.setVisibility(View.GONE);
        } else {
            layoutDescription.setVisibility(View.VISIBLE);
            tvDescription.setText(levelDetail.Description);
        }

        bestRank = levelDetail.BestRank;
        if (bestRank == null) {
            layoutBest.setVisibility(View.GONE);
        }else {
            layoutBest.setVisibility(View.VISIBLE);
            String bestName = Utils.Text.getTruncatedString(bestRank.Name, 16);
            tvBestName.setText(bestName);
            tvBestSchool.setText(bestRank.SchoolName);

            int hour = bestRank.ClearanceTime / 3600;
            int minute = bestRank.ClearanceTime % 3600 / 60;
            int second = bestRank.ClearanceTime % 60;
            String hourStr = (hour < 1) ? "" : hour + ":";
            String minuteStr = (minute < 10) ? "0" + minute : "" + minute;
            String secondStr = (second < 10) ? "0" + second : "" + second;

            String sumTime = hourStr + minuteStr + "'" + secondStr + "\"";
            tvBestTime.setText(sumTime);

            switch (bestRank.CompletionStatus) {
                case 10:
                    ivBestStar1.setImageResource(R.drawable.ic_star_selected);
                    ivBestStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivBestStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 20:
                    ivBestStar1.setImageResource(R.drawable.ic_star_selected);
                    ivBestStar2.setImageResource(R.drawable.ic_star_selected);
                    ivBestStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 30:
                    ivBestStar1.setImageResource(R.drawable.ic_star_selected);
                    ivBestStar2.setImageResource(R.drawable.ic_star_selected);
                    ivBestStar3.setImageResource(R.drawable.ic_star_selected);
                    break;
                default:
                    ivBestStar1.setImageResource(R.drawable.ic_star_unselected);
                    ivBestStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivBestStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
            }

            Glide
                    .with(this)
                    .load(Utils.getCorrectImageUrl(levelDetail.BestRank.Photo))
                    .centerCrop()
                    .crossFade()
                    .error(R.drawable.img_avatar_default)
                    .into(aivBestAvatar);
        }

        // 有前置关卡或场景
        if (levelDetail.PreScene != null || levelDetail.PreLevel != null) {
            layoutClosed.setVisibility(View.VISIBLE);

            if (levelDetail.PreLevel != null) {
                tvClosedTips.setText(getString(R.string.level_closed_tips, "关卡" + levelDetail.PreLevel.Title));

                btnGoToDependency.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.UI.isFastDoubleClick()) {
                            return;
                        }

                        Intent intent = new Intent();
                        intent.setClass(getActivity(), LevelActivity_.class);
                        intent.putExtra("levelId", levelDetail.PreLevel.Id);
                        startActivity(intent);
                    }
                });
            } else {
                tvClosedTips.setText(getString(R.string.level_closed_tips, "场景" + levelDetail.PreScene.Title));

                btnGoToDependency.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Utils.UI.isFastDoubleClick()) {
                            return;
                        }

                        ((LevelActivity_)getActivity()).isSceneUnlocked(levelDetail.PreScene.Id);
                    }
                });
            }
        }

        // 1: 硬过模式; 10: 闯关模式
        if (levelDetail.LevelType == 1) {
            layoutModeMustPass.setVisibility(View.VISIBLE);
            layoutModeCanPass.setVisibility(View.GONE);

            // 完美闯关
            if (levelDetail.CompletionStatus == 30) {
                layoutModeMustPassNotPass.setVisibility(View.GONE);
                layoutModeMustPassPerfectPass.setVisibility(View.VISIBLE);

                tvModeMustPassThroughTime.setText(levelDetail.getFormattedThroughTime());
                if (levelDetail.ClearanceTime < 1) {
                    tvModeMustPassClearanceTime.setVisibility(View.INVISIBLE);
                } else {
                    tvModeMustPassClearanceTime.setVisibility(View.VISIBLE);
                    tvModeMustPassClearanceTime.setText(getString(R.string.level_clearance_time, Utils.secondsToHumanReadable(levelDetail.ClearanceTime)));
                }
            } else {    // 未通关
                layoutModeMustPassNotPass.setVisibility(View.VISIBLE);
                layoutModeMustPassPerfectPass.setVisibility(View.GONE);
            }
        } else if (levelDetail.LevelType == 10) {
            layoutModeMustPass.setVisibility(View.GONE);
            layoutModeCanPass.setVisibility(View.VISIBLE);

            switch (levelDetail.CompletionStatus) {
                case 100:
                case 0:
                    tvModeCanPassNotPass.setVisibility(View.VISIBLE);
                    layoutModeCanPassPass.setVisibility(View.GONE);

                    ivModeCanPassStar1.setImageResource(R.drawable.ic_star_unselected);
                    ivModeCanPassStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivModeCanPassStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 10:
                    tvModeCanPassNotPass.setVisibility(View.GONE);
                    layoutModeCanPassPass.setVisibility(View.VISIBLE);

                    tvModeCanPassThroughTime.setText(levelDetail.getFormattedThroughTime());

                    ivModeCanPassStar1.setImageResource(R.drawable.ic_star_selected);
                    ivModeCanPassStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivModeCanPassStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 20:
                    tvModeCanPassNotPass.setVisibility(View.GONE);
                    layoutModeCanPassPass.setVisibility(View.VISIBLE);

                    tvModeCanPassThroughTime.setText(levelDetail.getFormattedThroughTime());

                    ivModeCanPassStar1.setImageResource(R.drawable.ic_star_selected);
                    ivModeCanPassStar2.setImageResource(R.drawable.ic_star_selected);
                    ivModeCanPassStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 30:
                    tvModeCanPassNotPass.setVisibility(View.GONE);
                    layoutModeCanPassPass.setVisibility(View.VISIBLE);

                    tvModeCanPassThroughTime.setText(levelDetail.getFormattedThroughTime());

                    ivModeCanPassStar1.setImageResource(R.drawable.ic_star_selected);
                    ivModeCanPassStar2.setImageResource(R.drawable.ic_star_selected);
                    ivModeCanPassStar3.setImageResource(R.drawable.ic_star_selected);

                    tvModeCanPassStatus.setText("恭喜你完美通关");
                    btnModeCanPassBegin.setText("再次闯关");
                    break;
                default:
                    tvModeCanPassNotPass.setVisibility(View.VISIBLE);
                    layoutModeCanPassPass.setVisibility(View.GONE);

                    ivModeCanPassStar1.setImageResource(R.drawable.ic_star_unselected);
                    ivModeCanPassStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivModeCanPassStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
            }

            circleProgressViewModeCanPass.setValue(levelDetail.CorrectRate * 100);
            tvPersent.setText((int)(levelDetail.CorrectRate * 100) + "");
            circleProgressViewModeCanPass.setUnitPosition(UnitPosition.RIGHT_BOTTOM);
            if (levelDetail.ClearanceTime < 1) {
                tvModeCanPassClearanceTime.setVisibility(View.INVISIBLE);
            } else {
                tvModeCanPassClearanceTime.setVisibility(View.VISIBLE);
                tvModeCanPassClearanceTime.setText(getString(R.string.level_clearance_time, Utils.secondsToHumanReadable(levelDetail.ClearanceTime)));
            }
        }

        // 0: 未开启; 10: 已开启
        if (levelDetail.LockStatus == 0) {
            // 关闭状态不显示任何闯关按钮
            btnModeCanPassBegin.setVisibility(View.GONE);
            btnModeMustPassBegin.setVisibility(View.GONE);
            btnModeMustPassAgain.setVisibility(View.GONE);
        }

        firstIntent = false;

    }

    @Click({R.id.frag_level_mode_can_pass_btn_begin,
            R.id.frag_level_mode_must_pass_btn_begin,
            R.id.frag_level_mode_must_pass_btn_again
    })
    void clickBeginBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        ((LevelActivity_)getActivity()).toQuestionPage();
    }



}
